
//引入网络库
import { get, post } from "../../util/util.js"

// 获取全局应用程序实例对象
var app = getApp();

// 创建页面实例对象
Page({
  /** 
   * 页面名称
   */
  name: "search",
  /**
   * 页面的初始数据
   */

  data: {
    keyStr: '',
    //个人特征 
    skillSize: 1,
    skillKey: '',
    skillArr: [],
    //颜值
    faceSize: 1,
    faceKey: '',
    faceArr: [],
    //性别
    sex: "",
    //年龄段
    age_groupSize: 1,
    age_groupKey: '',
    age_groupArr: [],
    //身高 
    heightSize: 1,
    heightKey: '',
    heightArr: [],
    //体型 
    shapeSize: 1,
    shapeKey: '',
    shapeArr: [],
    //语言
    languageSize: 1,
    languageKey: '',
    languageArr: [],
    //特长
    featureSize: 1,
    featureKey: '',
    featureArr: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },

  onLoad: function (o) {
    var that = this;
    //艺人配置
    post("/tag/getConfig", {}).then(data => {
      //身高 
      var heightData = [];
      var heightStr = [];
      for (var key in data.data.height) {
        heightData.push(key);
        heightStr.push(data.data.height[key]);
      }
      var heightJson = [];
      for (var i = 0; i < heightStr.length; i++) {
        var j = {};
        j.val = heightData[i];
        j.name = heightStr[i];
        j.value = i;
        j.checked = false;
        heightJson.push(j);
      }
      // 体型 
      var shapeData = [];
      var shapeStr = [];
      for (var key in data.data.shape) {
        shapeData.push(key);
        shapeStr.push(data.data.shape[key]);
      }
      var shapeJson = [];
      for (var i = 0; i < shapeStr.length; i++) {
        var j = {};
        j.val = shapeData[i];
        j.name = shapeStr[i];
        j.value = i;
        j.checked = false;
        shapeJson.push(j);
      }
      //年龄段  
      var age_groupData = [];
      var age_groupStr = [];
      for (var key in data.data.age) {
        age_groupData.push(key);
        age_groupStr.push(data.data.age[key]);
      }
      var age_groupJson = [];
      for (var i = 0; i < age_groupStr.length; i++) {
        var j = {};
        j.val = age_groupData[i];
        j.name = age_groupStr[i];
        j.value = i;
        j.checked = false;
        age_groupJson.push(j);
      }
      //个人特征  
      var skillData = [];
      var skillStr = [];
      for (var key in data.data.skill) {
        for (var keyItem in data.data.skill[key]) {
          skillData.push(keyItem);
          skillStr.push(data.data.skill[key][keyItem]);
        }
      }
      var skillJson = [];
      for (var i = 0; i < skillStr.length; i++) {
        var j = {};
        j.val = skillData[i];
        j.name = skillStr[i];
        j.value = i;
        j.checked = false;
        skillJson.push(j);
      }

      //特长  
      var featureData = [];
      var featureStr = [];
      for (var key in data.data.feature) {
        featureData.push(key);
        featureStr.push(data.data.feature[key]);
      }
      var featureJson = [];
      for (var i = 0; i < featureStr.length; i++) {
        var j = {};
        j.val = featureData[i];
        j.name = featureStr[i];
        j.value = i;
        j.checked = false;
        featureJson.push(j);
      }
      //语言  
      var languageData = [];
      var languageStr = [];
      for (var key in data.data.language) {
        languageData.push(key);
        languageStr.push(data.data.language[key]);
      }
      var languageJson = [];
      for (var i = 0; i < languageStr.length; i++) {
        var j = {};
        j.val = languageData[i];
        j.name = languageStr[i];
        j.value = i;
        j.checked = false;
        languageJson.push(j);
      }
      //颜值  
      var faceData = [];
      var faceStr = [];
      for (var key in data.data.face) {
        faceData.push(key);
        faceStr.push(data.data.face[key]);
      }
      var faceJson = [];
      for (var i = 0; i < faceStr.length; i++) {
        var j = {};
        j.val = faceData[i];
        j.name = faceStr[i];
        j.value = i;
        j.checked = false;
        faceJson.push(j);
      }

      that.setData({
        // uid: o.uid,

        heightArr: heightJson,

        shapeArr: shapeJson,

        age_groupArr: age_groupJson,

        skillArr: skillJson,

        featureArr: featureJson,

        languageArr: languageJson,

        faceArr: faceJson,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  }
  ,
  /**
   * 获取点击 身高 字段
   */
  getSg: function (e) {
    var that = this;
    var height = that.data.heightArr;
    var size = that.data.heightSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.heightArr[index].val + "身高选择" + that.data.heightArr[index].checked);
    if (height[index].checked) {
      size--;
      height[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        height[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      heightArr: height,
      heightSize: size,
    })
  },
  radioChange: function (e) {
    console.log("性别" + e.detail.value);
    var that = this;
    that.setData({
      sex: e.detail.value
    })
  },

  keyStrInput: function (e) {
    this.setData({
      keyStr: e.detail.value
    })
  },

  /**
    * 获取点击 体型 字段
    */
  getTx: function (e) {
    var that = this;
    var shape = that.data.shapeArr;
    var size = that.data.shapeSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.shapeArr[index].val + "年龄段选择" + that.data.shapeArr[index].checked);
    if (shape[index].checked) {
      size--;
      shape[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        shape[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      shapeArr: shape,
      shapeSize: size,
    })
  },

  /**
  * 获取点击 年龄段 字段
  */
  getNld: function (e) {
    var that = this;
    var age_group = that.data.age_groupArr;
    var size = that.data.age_groupSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.age_groupArr[index].val + "年龄段选择" + that.data.age_groupArr[index].checked);
    if (age_group[index].checked) {
      size--;
      age_group[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        age_group[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      age_groupArr: age_group,
      age_groupSize: size,
    })
  },
  /**
  * 获取点击 特征 字段
  */
  getTz: function (e) {
    var that = this;
    var skillShenGao = that.data.skillArr;
    var size = that.data.skillSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.skillArr[index].val + "特征选择" + that.data.skillArr[index].checked);
    if (skillShenGao[index].checked) {
      size--;
      skillShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        skillShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      skillArr: skillShenGao,
      skillSize: size,
    })
  },

  /**
* 获取点击 特长 字段
*/
  getTc: function (e) {
    var that = this;
    var featureShenGao = that.data.featureArr;
    var size = that.data.featureSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.featureArr[index].val + "特长选择" + that.data.featureArr[index].checked);
    if (featureShenGao[index].checked) {
      size--;
      featureShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        featureShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      featureArr: featureShenGao,
      featureSize: size,
    })
  },
  /**
* 获取点击 语言 字段
*/
  getYy: function (e) {
    var that = this;
    var languageShenGao = that.data.languageArr;
    var size = that.data.languageSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.languageArr[index].val + "语言选择" + that.data.languageArr[index].checked);
    if (languageShenGao[index].checked) {
      size--;
      languageShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        languageShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      languageArr: languageShenGao,
      languageSize: size,
    })
  },

  /**
* 获取点击 颜值 字段
*/
  getYz: function (e) {
    var that = this;
    var faceShenGao = that.data.faceArr;
    var size = that.data.faceSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.faceArr[index].val + "颜值选择" + that.data.faceArr[index].checked);
    if (faceShenGao[index].checked) {
      size--;
      faceShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        faceShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      faceArr: faceShenGao,
      faceSize: size,
    })
  },
  searchSub: function () {
    var that = this;
    var tixing = [];
    for (var i = 0; i < that.data.shapeArr.length; i++) {
      if (that.data.shapeArr[i].checked) {
        tixing.push(parseInt(that.data.shapeArr[i].val));
      }
    }
    var shengao = [];
    for (var i = 0; i < that.data.heightArr.length; i++) {
      if (that.data.heightArr[i].checked) {
        shengao.push(parseInt(that.data.heightArr[i].val));
      }
    }
    var nianling = [];
    for (var i = 0; i < that.data.age_groupArr.length; i++) {
      if (that.data.age_groupArr[i].checked) {
        nianling.push(parseInt(that.data.age_groupArr[i].val));
      }
    }
    var yanzhi = [];
    for (var i = 0; i < that.data.faceArr.length; i++) {
      if (that.data.faceArr[i].checked) {
        yanzhi.push(parseInt(that.data.faceArr[i].val));
      }
    }
    var tezheng = [];
    for (var i = 0; i < that.data.skillArr.length; i++) {
      if (that.data.skillArr[i].checked) {
        tezheng.push(parseInt(that.data.skillArr[i].val));
      }
    }
    console.log("特征传递参数=======" + JSON.stringify(tezheng));
    var techang = [];
    for (var i = 0; i < that.data.featureArr.length; i++) {
      if (that.data.featureArr[i].checked) {
        techang.push(parseInt(that.data.featureArr[i].val));
      }
    }
    console.log("特长传递参数=======" + JSON.stringify(techang));
    var yuyan = [];
    for (var i = 0; i < that.data.languageArr.length; i++) {
      if (that.data.languageArr[i].checked) {
        yuyan.push(parseInt(that.data.languageArr[i].val));
      }
    }
    console.log("语言传递参数=======" + JSON.stringify(yuyan));
    var param = {};
    if (that.data.keyStr != "") {
      param.key = that.data.keyStr;
    }
    if (that.data.sex != "") {
      param.sex = parseInt(that.data.sex);
    }
    if (tixing.length > 0) {
      param.shape = JSON.stringify(tixing);//
    }
    if (nianling.length > 0) {
      param.age_group = JSON.stringify(nianling);//
    }
    if (shengao.length > 0) {
      param.height = JSON.stringify(shengao);//
    }
    if (yanzhi.length > 0) {
      param.face = JSON.stringify(yanzhi);
    }
    if (techang.length > 0) {
      param.feature = JSON.stringify(techang);
    }
    if (tezheng.length > 0) {
      param.skill = JSON.stringify(tezheng);
    }
    if (yuyan.length > 0) {
      param.language = JSON.stringify(yuyan);
    }
    // param.page = 1;
    // param.pagesize = 10;
    console.log("搜索结果===传递===" + JSON.stringify(param));
    wx.navigateTo({
      url: '../searchresult/searchresult?param=' + JSON.stringify(param),
    })
  },
})

